package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.NotifyStatusEntity;
import com.zw.yzk.kabu.data.network.entity.NotifySwitchStatusEntity;
import com.zw.yzk.kabu.data.network.task.NotifySwitchTask;
import com.zw.yzk.kabu.data.network.task.NotifyTask;
import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.view.view.SettingsView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/5/19.
 */

public class SettingsPresenter implements Presenter {

    private UseCase notifySwitch;
    private UseCase getNotifyStatus;
    private AppCompatActivity activity;
    private SettingsView settingsView;

    @Inject
    public SettingsPresenter(@Named("setNotifyUseCase")UseCase notifySwitch,
                             @Named("getNotifyUseCase")UseCase getNotifyStatus,
                             AppCompatActivity activity) {
        this.notifySwitch = notifySwitch;
        this.getNotifyStatus = getNotifyStatus;
        this.activity = activity;
    }

    public void setSwitchStatus(int type, int status) {
        notifySwitch.execute(new BaseSubscriber(activity) {
            @Override
            public String addErrorMessage() {
                return "";
            }
        }, new NotifyTask(type, status));
    }

    public void setView(SettingsView view) {
        this.settingsView = view;
    }

    public void getSwitchStatus() {
        getNotifyStatus.execute(new SwitchStatusSubscriber(activity), new Task());
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        notifySwitch.unSubscribe();
        getNotifyStatus.unSubscribe();
    }

    private class SwitchStatusSubscriber extends BaseSubscriber<NotifyStatusEntity> {

        public SwitchStatusSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(NotifyStatusEntity entity) {
            super.onNext(entity);
            settingsView.getNotifyStatus(entity.result.isExhibitionNotified == 1,
                    entity.result.isConcernNotified == 1, entity.result.isCommentNotified == 1);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
        }

        @Override
        public String addErrorMessage() {
            return null;
        }
    }
}
